package com.dxysun.springboot.controller;

import com.dxysun.springboot.domain.PushToken;
import com.dxysun.springboot.dto.MyUser;
import com.dxysun.springboot.repository.PushTokenRepo;
import com.dxysun.springboot.service.MyService;
import com.dxysun.springboot.dto.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Controller
public class HelloController
{

    @Autowired
    private MyService myService;

    @Autowired
    private PushTokenRepo pushTokenRepo;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam(required = false, defaultValue = "world") String name)
    {
        return "hello " + name;
    }


    @RequestMapping("/nosCallBack")
    @ResponseBody
    public User nosCallBack(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String authorization = request.getHeader("Authorization");

        System.out.println("authorization:" + authorization);
        String body = getRequestBody(request);
        System.out.println("body:" + body);
        User user = new User();
        user.setName("123");
        user.setRemark("test");
//        response.setContentType("text/plain;charset=UTF-8");
        // 直接响应数据
//        response.getWriter().print(JSONObject.toJSONString(user));
//        response.getWriter().flush();

//        return JSONObject.toJSONString(user);
        return user;
    }

    private String getRequestBody(HttpServletRequest request)
    {
        String inputLine;
        StringBuilder str = new StringBuilder();
        try
        {
            BufferedReader br = request.getReader();
            if (br == null)
            {
                return "";
            }
            while ((inputLine = br.readLine()) != null)
            {
                str.append(inputLine);
            }
            br.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return str.toString();
    }

    @RequestMapping("/insert")
    @ResponseBody
    public String insert(String name)
    {
        PushToken pushToken = new PushToken();
        pushToken.setAccountId("1234");
        pushToken.setCoreAccountId("123456");
        pushToken.setAppVersion("1234");
        pushToken.setDeviceToken("avcd");
        pushToken.setPushChannel("qwer");
        pushTokenRepo.insertPushMessage(pushToken);
        return "hello " + name;
    }

    @RequestMapping("/queryAccountChannelMap")
    @ResponseBody
    public List<PushToken> queryAccountChannelMap(String accountIds)
    {
        return pushTokenRepo.queryAccountChannelMap(Arrays.asList(accountIds.split(",")));
    }

    @RequestMapping("/execute")
    @ResponseBody
    public String execute(String source)
    {
        System.out.println("execute job " + source);
        return "execute job " + source;
    }

    @RequestMapping("/userhello")
    @ResponseBody
    public User getInfo(MyUser user)
    {
        String str = myService.getServiceInfo("HelloController", user.getName());
//        System.out.println("HelloController:" + str);
        user.setInfo(str);
        return user;
    }
}
